package com.bree.erp.jsf.view;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.apache.log4j.Logger;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.LazyDataModel;

import com.bree.erp.entity.PurchaseOrder;
import com.bree.erp.entity.Status;
import com.bree.erp.jsf.datatable.LazyBreeDataModel;
import com.bree.erp.jsf.service.PurchaseOrderWrapperService;
import com.bree.erp.jsf.service.wrapper.PurchaseOrderWrapperServiceBean;
import com.bree.erp.service.PurchaseOrderService;

@ManagedBean(name="dtPurchaseOrderLazyView")
@ViewScoped
public class PurchaseOrderLazyView {
	

	private static Logger LOG = Logger.getLogger(PurchaseOrderLazyView.class);
	
	@EJB
	private PurchaseOrderService poService;

	private LazyDataModel<PurchaseOrder> lazyModel;
	private LazyDataModel<PurchaseOrder> receivableLazyModel;
	private LazyDataModel<PurchaseOrder> withdrawalsLazyModel;
	private LazyDataModel<PurchaseOrder> pickupsLazyModel;
	private LazyDataModel<PurchaseOrder> archivesLazyModel;
	
	private PurchaseOrder selectedItem;
	
	@PostConstruct
	public void init() {
		lazyModel = new LazyBreeDataModel<PurchaseOrder, PurchaseOrderService, Long>(poService, 0L);
		receivableLazyModel = new LazyBreeDataModel<PurchaseOrder, PurchaseOrderWrapperService, Long>(new PurchaseOrderWrapperServiceBean(poService, Status.CONFIRMED, Status.PARTIAL), 0L);
		withdrawalsLazyModel = new LazyBreeDataModel<PurchaseOrder, PurchaseOrderWrapperService, Long>(new PurchaseOrderWrapperServiceBean(poService, Status.RECEIVED), 0L);
		pickupsLazyModel = new LazyBreeDataModel<PurchaseOrder, PurchaseOrderWrapperService, Long>(new PurchaseOrderWrapperServiceBean(poService, Status.WITHDRAW), 0L);
		archivesLazyModel = new LazyBreeDataModel<PurchaseOrder, PurchaseOrderWrapperService, Long>(new PurchaseOrderWrapperServiceBean(poService, Status.CLOSED), 0L);
	}
	
	public void onRowSelect(SelectEvent event) {
		PurchaseOrder purchaseOrder = (PurchaseOrder) event.getObject();
		 FacesMessage msg = new FacesMessage("Item Selected", purchaseOrder.getId() + "");
	     FacesContext.getCurrentInstance().addMessage(null, msg);
	}

	/**
	 * @return the lazyModel
	 */
	public LazyDataModel<PurchaseOrder> getLazyModel() {
		
		LOG.debug("Get PO lazy model: " + lazyModel);
		return lazyModel;
	}

	/**
	 * @param lazyModel the lazyModel to set
	 */
	public void setLazyModel(LazyDataModel<PurchaseOrder> lazyModel) {
		this.lazyModel = lazyModel;
	}

	/**
	 * @return the selectedItem
	 */
	public PurchaseOrder getSelectedItem() {
		return selectedItem;
	}

	/**
	 * @param selectedItem the selectedItem to set
	 */
	public void setSelectedItem(PurchaseOrder selectedItem) {
		this.selectedItem = selectedItem;
	}

	/**
	 * @return the receivableLazyModel
	 */
	public LazyDataModel<PurchaseOrder> getReceivableLazyModel() {
		return receivableLazyModel;
	}

	/**
	 * @return the withdrawalsLazyModel
	 */
	public LazyDataModel<PurchaseOrder> getWithdrawalsLazyModel() {
		return withdrawalsLazyModel;
	}

	/**
	 * @return the pickupsLazyModel
	 */
	public LazyDataModel<PurchaseOrder> getPickupsLazyModel() {
		return pickupsLazyModel;
	}

	/**
	 * @return the archivesLazyModel
	 */
	public LazyDataModel<PurchaseOrder> getArchivesLazyModel() {
		return archivesLazyModel;
	}
}
